package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.beauty.domain.BeautyCoupon;
import com.linq.cool.beauty.dto.BeautyCouponDTO;
import com.linq.cool.beauty.dto.BeautyCouponQueryDTO;
import com.linq.cool.beauty.service.BeautyCouponProductCategoryService;
import com.linq.cool.beauty.service.BeautyCouponService;
import com.linq.cool.beauty.service.BeautyProductCategoryService;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/7 03:27
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("coupon")
public class BeautyCouponController extends BaseController {
    @Autowired
    private BeautyCouponService beautyCouponService;
    @Autowired
    private BeautyProductCategoryService beautyProductCategoryService;
    @Autowired
    private BeautyCouponProductCategoryService beautyCouponProductCategoryService;


    @PreAuthorize(hasPerm = "beauty:coupon:list")
    @GetMapping("list")
    public Result<?> list(BeautyCouponQueryDTO dto) {
        IPage<BeautyCoupon> page = beautyCouponService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }


    @Log(moduleTitle = "卡券管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "beauty:coupon:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody BeautyCouponDTO dto) {
        return this.toResult(beautyCouponService.saveCoupon(dto));
    }

    @GetMapping("{couponId}")
    public Result<?> getInfo(@PathVariable("couponId") Long couponId) {
        return Result.ok(beautyCouponService.getInfo(couponId));
    }

    @Log(moduleTitle = "卡券管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "beauty:coupon:remove")
    @DeleteMapping("{couponIds}")
    public Result<?> remove(@PathVariable("couponIds") List<Long> couponIds) {
        return this.toResult(beautyCouponService.removeCoupon(couponIds));
    }

    /**
     * 查询指定商品可以使用的优惠券
     * 全场通用优惠券
     * 该商品所在分类的优惠券
     * 该商品的优惠券
     *
     * @param productId
     *
     * @return
     */
    @GetMapping("getProductCoupons/{productId}")
    public Result<?> getProductCoupons(@PathVariable("productId") Long productId) {
        return Result.ok(beautyCouponService.getProductCoupons(productId));
    }

    /**
     * 下架卡券
     */
    @Log(moduleTitle = "卡券管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:coupon:remove")
    @PutMapping("down/{couponId}")
    public Result<?> downCoupon(@PathVariable("couponId") Long couponId) {
        BeautyCoupon coupon = beautyCouponService.getById(couponId);
        if (null == coupon) {
            return Result.ok("该卡券不存在");
        }
        coupon.setStatus(UserConstants.StateEnum.COUPON_TIMEOUT.getCode()).setGmtModified(new Date()).setModifier(SecurityUtils.getUserId());
        return this.toResult(beautyCouponService.updateById(coupon));
    }

}
